package codeCreate.strategyService.impl;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import codeCreate.CodeCreateUtil;
import codeCreate.strategyService.StrategyService;
import codeCreate.util.ControllerResolveUtil;
import codeCreate.util.MsWordEdit;

public class CreateApiDocumentStrategy implements StrategyService<String> {
    // Word格式字段
    private String filePath = null;
    private String fontSize = "10.5";
    private String fontColor = "00000000";
    private String fontName = "宋体";
    private String backColor = "4772300";
    private float defaultBoundLineWidth = 0.5f;
    private int defaultTableHeightWidth = 18;
    // 文档默认值字段
    private String ip_port = "address:port";
    private String httpPrefix = "HTTP://";
    private String requestType = "POST";
    private String contentType = "content-type：application/ json;accept：application/ json";
    private String request_type = "requestType";
    private String response_type = "responseType";
    // controller类相关常量字段
    private String controller_path = "controllerPath";
    private String methods_info = "methodsInfo";
    private String method_introduction = "methodIntroduction";
    private String contorller_introduction = "controllerIntroduction";
    // 请求类/返回类字段
    private static String name = "name";
    private static String is_null = "isNull";
    private static String field_name = "fieldName";
    private static String length = "length";
    private static String introduction = "introduction";
    private static String field_type = "fieldType";
    // 文档自定义信息
    private String[] requestTemplate = { field_name, is_null, field_type, length, name, introduction };
    String[] requestColumn = { "名称", "约束", "类型", "长度", "描述", "说明" };
    private String[] responseTemplate = { field_name, is_null, field_type, length, name, introduction };
    String[] responseColumn = { "名称", "约束", "类型", "长度", "描述", "说明" };
    // 类私有字段
    private CodeCreateUtil codeCreateUtil = new CodeCreateUtil();
    private ControllerResolveUtil controllerResolveUtil;

    public CreateApiDocumentStrategy(String filePath, Class<?>[] clazz) {
        this.filePath = filePath;
        this.controllerResolveUtil = new ControllerResolveUtil(clazz);
    }

    @Override
    public String create() throws Exception {
        MsWordEdit msWordManager = new MsWordEdit();
        try {
            File file = new File(filePath);
            if (file.exists()) {
                file.delete();
            }
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            msWordManager.openDocument(filePath);
            Iterator<Entry<Class<?>, Map<String, Object>>> iterator = controllerResolveUtil.resolve().entrySet()
                    .iterator();
            int controllerNumber = 1;
            while (iterator.hasNext()) {
                Entry<Class<?>, Map<String, Object>> controllerEntry = iterator.next();
                // 控制器信息map
                Map<String, Object> controllerMap = controllerEntry.getValue();
                String controllerPath = (String) controllerMap.get(controller_path);
                String contorllerIntroduction = (String) controllerMap.get(contorller_introduction);
                @SuppressWarnings("unchecked")
                Iterator<Entry<String, Object>> methodIterator = ((Map<String, Object>) controllerMap.get(methods_info))
                        .entrySet().iterator();
                int methodNumber = 1;
                msWordManager.setFont(true, false, false, "00000000", "14", "宋体");
                msWordManager.insertText(controllerNumber + "   " + contorllerIntroduction);
                msWordManager.Enter();
                // 控制器信息结束
                while (methodIterator.hasNext()) {
                    Entry<String, Object> entry = methodIterator.next();
                    String methodPath = entry.getKey();
                    @SuppressWarnings("unchecked")
                    Map<String, Object> methodMap = (Map<String, Object>) entry.getValue();
                    String introduction = (String) methodMap.get(method_introduction);
                    Class<?> requestClass = (Class<?>) methodMap.get(request_type);
                    Class<?> responseClass = (Class<?>) methodMap.get(response_type);
                    List<String[]> requestClassInfo = controllerResolveUtil.resolveBeanMapList2Array(requestClass,
                            requestTemplate);
                    List<String[]> responseClassInfo = controllerResolveUtil.resolveBeanMapList2Array(responseClass,
                            responseTemplate);
                    int requestInfoRowNumber = requestClassInfo.size();
                    int requestInfoColNumber = requestTemplate.length;
                    int responseInfoRowNumber = responseClassInfo.size();
                    int responseInfoColNumber = responseTemplate.length;
                    String pointMN = "." + methodNumber;

                    msWordManager.setParaFormat(0, 1);
                    msWordManager.setFont(true, false, false, "00000000", "10.5", "宋体");
                    msWordManager.insertText(controllerNumber + pointMN + "  " + introduction);
                    msWordManager.Enter();
                    msWordManager.setFont(true, false, false, "00000000", "12", "宋体");
                    msWordManager.insertText(controllerNumber + pointMN + ".1   【基本描述】");
                    msWordManager.Enter();
                    msWordManager.createTable("", 2, 2, defaultBoundLineWidth, defaultTableHeightWidth);
                    msWordManager.setTableWidth(1, 1, 60);
                    msWordManager.setTableWidth(1, 2, 350);
                    msWordManager.setTableColGroundColor(1, 1, backColor);
                    String[] txts = { "接口名称", "接口描述" };
                    msWordManager.putTxtToCellHeadRow(1, 1, 1, txts, 2, 2, fontColor, fontSize, fontName);
                    msWordManager.putTxtToCell(1, 1, 2, introduction + "接口", 2, 2);
                    msWordManager.putTxtToCell(1, 2, 2, introduction, 2, 2);
                    msWordManager.insertText(controllerNumber + pointMN + ".2   【请求信息】  ");
                    msWordManager.Enter();
                    msWordManager.createTable("", 6, 2, defaultBoundLineWidth, defaultTableHeightWidth);
                    msWordManager.setTableWidth(2, 1, 60);
                    msWordManager.setTableWidth(2, 2, 350);
                    msWordManager.setTableColGroundColor(2, 1, backColor);
                    String[] txts2 = { "前提要求", "请求类型", "请求地址", "请求方式", "报文格式", "接口参数" };
                    msWordManager.putTxtToCellHeadRow(2, 1, 1, txts2, 6, 2, fontColor, fontSize, fontName);
                    msWordManager.putTxtToCell(1, 1, 2, "用户已登录", 6, 2);
                    msWordManager.putTxtToCell(1, 2, 2, "HTTP|HTTPS", 6, 2);
                    msWordManager.putTxtToCell(1, 3, 2, httpPrefix + ip_port + controllerPath + methodPath, 6, 2);
                    msWordManager.putTxtToCell(1, 4, 2, requestType, 6, 2);
                    msWordManager.putTxtToCell(1, 5, 2, contentType, 6, 2);
                    msWordManager.Enter();
                    msWordManager.insertText(controllerNumber + pointMN + ".3   【请求参数说明】     ");
                    msWordManager.Enter();
                    msWordManager.createTable("", requestInfoRowNumber + 1, requestInfoColNumber, defaultBoundLineWidth,
                            defaultTableHeightWidth);
                    msWordManager.setTableRowGroundColor(3, 1, backColor);
                    msWordManager.putTxtToCellHeadCol(3, 1, 1, requestColumn, requestInfoRowNumber + 1,
                            requestInfoColNumber, fontColor, fontSize, fontName);
                    msWordManager.putListTxtToCell(3, 2, 1, requestClassInfo, requestInfoRowNumber + 1,
                            requestInfoRowNumber, fontColor, fontSize, fontName);
                    msWordManager.insertText(controllerNumber + pointMN + ".4   【请求示例】     ");
                    msWordManager.Enter();
                    msWordManager.insertText(httpPrefix + ip_port + controllerPath + methodPath);
                    msWordManager.Enter();
                    msWordManager.insertText(codeCreateUtil.createBeanJson(requestClass));
                    msWordManager.Enter();
                    msWordManager.insertText(controllerNumber + pointMN + ".5   【响应消息】");
                    msWordManager.Enter();
                    msWordManager.createTable("", responseInfoRowNumber + 1, responseInfoColNumber,
                            defaultBoundLineWidth, defaultTableHeightWidth);
                    msWordManager.setTableRowGroundColor(4, 1, backColor);
                    msWordManager.putTxtToCellHeadCol(3, 1, 1, responseColumn, responseInfoRowNumber + 1,
                            responseInfoColNumber, fontColor, fontSize, fontName);
                    msWordManager.putListTxtToCell(3, 2, 1, responseClassInfo, responseInfoRowNumber + 1,
                            responseInfoRowNumber, fontColor, fontSize, fontName);
                    msWordManager.insertText("样例：");
                    msWordManager.Enter();
                    msWordManager.insertText(codeCreateUtil.createBeanJson(responseClass));
                    msWordManager.Enter();
                    methodNumber++;
                }
                controllerNumber++;
            }
            msWordManager.closeDocument(-1);
            System.out.println("end");
        } finally {
            msWordManager.close();
        }
        return null;
    }

    public static void main(String[] args) {

    }
}
